//
//  TrtcEngine.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/10.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

public class TrtcEngine: NSObject {
    static let shared = TrtcEngine()
    
    class func addStandDFs(inWindow:NSWindow) {
        let window = NSWindow.init(contentViewController: StandFsViewController())
        window.minSize = NSSize.init(width: kStandWidth, height: kStandMinHight)
        window.maxSize = NSSize.init(width: kStandMaxWidth, height: kStandMaxHeight)
        window.setNoTabarAndMask()
        window.styleMask = [.resizable]
        window.orderInFont(onWindow: inWindow, sender: nil)
        //inWindow.addChildWindow(window, ordered: NSWindow.OrderingMode.above)
        //window.setFrameOrigin(NSPoint.init(x: KSCREENWIDTH!-500, y: KSCREENHEIGHT!-300))
    }
    
    class func removeDFs(inWindow:NSWindow) {
        TrtcEngine.close(classs: [StandFsViewController.classForCoder()])
    }
    
    class func standOrderFront(inWindow:NSWindow?) {
        let miniVc = UserStandViewController()
        let window = NSWindow.init(contentViewController: miniVc)
        window.minSize = NSSize.init(width: kStandWidth, height: kStandMinHight)
        window.maxSize = NSSize.init(width: kStandMaxWidth, height: kStandMaxHeight)
        window.setNoTabar()
        window.orderInFont(onWindow: inWindow, sender: nil)
        //window.setFrameOrigin(NSPoint.init(x: KSCREENWIDTH!-500, y: KSCREENHEIGHT!-300))
        
    }
    
    class func standClose() {
        TrtcEngine.close(classs: [UserStandViewController.classForCoder()])
    }
    
    
    class func hostTrtcOrderFront() {
        if Td.sh().scene == .live {
            self.trtcLiveOrderFront()
        }else {
            self.trtcOrderFront()
        }
    }
    
    class func joinTrtcOrderFront() {
        if Td.sh().scene == .live {
            self.adcOrderFront()
        }else {
            self.trtcOrderFront()
        }
    }
    
    class func enterScreenShare(info:TRTCScreenCaptureSourceInfo) {
        TrtcEngine.close(classs: [
                            TCSdColloctViewController.classForCoder(),
                            //TCMeetEngineViewController.classForCoder(),
                            //TCLiveEngineViewController.classForCoder(),
                            TCSdMenuViewController.classForCoder()])
        let shareVc = TCSdScreenViewController.init(source: info)
        let window = NSWindow.init(contentViewController: shareVc)
        window.setScreen()
        window.orderFront(nil)
        //TrtcEngine.standOrderFront(inWindow: nil)
        TrtcEngine.addShareTo(inWindow: window, role: OneRole.host, info: info)
        
        if let userid = Td.sh().params.userId {
            Td.sh().sd.isSding = true
            Td.sh().sd.sdUserId = nil
            NotificationCenter.default.post(name: MTSubStreamNotification, object: StreamMode.init(userId: userid, available: true))
        }
    }
    
    
    class func exitScreenShare() {
        TrtcEngine.close(classs: [UserStandViewController.classForCoder(),TCSdScreenViewController.classForCoder(),TCSdColloctViewController.classForCoder()])
        
        //TrtcEngine.hostTrtcOrderFront()
        if let userid = Td.sh().params.userId {
            Td.sh().sd.isSding = false
            Td.sh().sd.sdUserId = nil
            NotificationCenter.default.post(name: MTSubStreamNotification, object: StreamMode.init(userId: userid, available: false))
        }
    }
    
    class func removeShareTo(inWindow:NSWindow) {
        TrtcEngine.close(classs: [TCSdMenuViewController.classForCoder()])
    }
    
    class func addShareTo(inWindow:NSWindow,role:OneRole,info:TRTCScreenCaptureSourceInfo) {
        let window = NSWindow.init(contentViewController: TCSdMenuViewController.init(role: role,source:info))
        window.setNoBackground()
        window.delegate = TrtcEngine.shared
        window.orderInFont(onWindow: inWindow,isCenter:false,sender: nil)
        window.direction(in:.top)
    }
    
    /*
    切换到观众
    */
    class func switchRoleTo(isAdc:Bool) {
        Td.sh().lc.isAdc = isAdc
        TrtcEngine.closeDetory(classs: [TCMeetEngineViewController.classForCoder(),TCLiveEngineViewController.classForCoder(),TCAudienceViewController.classForCoder()])
        if isAdc == true {
            self.adcOrderFront()
        }else {
            self.trtcLiveOrderFront()
        }
    }
    
    class func rtmpOrderFront(inWindow:NSWindow?) {
        let control = RtmpSetViewController()
        let window = WEWindow.init(contentViewController: control)
        window.webView = control.webView
        window.orderInFont(onWindow: inWindow, sender: nil)
    }
    
    class func inviteOrderFront() {
        let control = ShareViewController()
        let window = NSWindow.init(contentViewController: control)
        window.setDefault()
        window.lorderFront(nil)
    }
    
    class func meetingPasswordOrderFront(nextBlock:ResultString?,closeBlock:os_block_t?) {
        let control = MeetingPasswordViewController.init()
        control.nextBlock = nextBlock
        control.closeBlock = closeBlock
        let window = NSWindow.init(contentViewController: control)
        window.lorderFront(nil)
    }
    
    class func adcOrderFront() {
        let window = ADWindow.init(contentViewController: TCAudienceViewController())
        window.minSize = NSSize.init(width: kAdcWinWidth*0.8, height: kAdcWinHeight*0.76)
        window.lorderFront(nil)
    }
    
    class func trtcOrderFront() {
        let controller = TCMeetEngineViewController()
        let window = TCWindow.init(contentViewController: controller)
        window.minSize = NSSize.init(width: kTRTCWinWidth*0.92, height: kTRTCWinHeight*0.86)
        window.delegate = controller
        window.lorderFront(nil)
    }
    
    class func trtcLiveOrderFront() {
        let controller = TCLiveEngineViewController()
        let window = TCWindow.init(contentViewController: controller)
        window.minSize = NSSize.init(width: kTRTCWinWidth*0.92, height: kTRTCWinHeight*0.86)
        window.delegate = controller
        window.lorderFront(nil)
    }
    
    
    class func enter() {
        TrtcEngine.close(classs: [
            //JoinViewController.classForCoder(),
            //MainViewController.classForCoder(),
            ShareViewController.classForCoder(),
        ])
        NSApp.windows.first?.close()
        //WssEngine.shared.connect()
    }
    
    
    class func exit() {
        TrtcEngine.close(classs: [
            TCMeetEngineViewController.classForCoder(),
            TCLiveEngineViewController.classForCoder(),
            TCAudienceViewController.classForCoder(),
            TCSdColloctViewController.classForCoder(),
            TRCTSetViewController.classForCoder(),
            TCSdScreenViewController.classForCoder(),
            TCSdMenuViewController.classForCoder(),
            TCMeetUSEREngViewController.classForCoder(),
            TCLiveUSEREngViewController.classForCoder(),
            TCAudienceUSEREngViewController.classForCoder(),
            UserStandViewController.classForCoder(),
            StandFsViewController.classForCoder(),
            TDSdSetViewController.classForCoder(),
            ShareViewController.classForCoder(),
        ])
        (NSApp.delegate as? AppDelegate)?.windowController.showWindow(nil)
        //WssEngine.shared.disconnect()
        //NSApp.windows.first?.makeKeyAndOrderFront(nil)
    }
    
    class func showPreferences(onWindow:NSWindow?) {
        let controller = SetEngineViewController()
        let window = PFWindow.init(contentViewController: controller)
        window.title = "基本设置"
        window.selectBlock = { item in
            controller.switchTo(index: item.tag)
        }
        window.centerIn(window: onWindow)
        window.lorderFront(nil)
    }
    
    class func getWindow(cls:AnyClass) -> NSWindow? {
        for (_,window) in NSApplication.shared.windows.enumerated(){
            if window.contentViewController != nil {
                if window.contentViewController?.isKind(of: cls) == true  {
                    return window
                }
            }
        }
        return nil
    }
    
    class func closeDetory(classs:[AnyClass]) {
        let windows = NSApplication.shared.windows
        windows.forEach { (win) in
            classs.forEach { (clas) in
                if win.contentViewController?.isKind(of: clas) == true {
                    win.contentViewController = nil
                    win.orderOut(nil)
                    win.close()
                }
            }
        }
    }
    
    class func close(classs:[AnyClass]) {
        let windows = NSApplication.shared.windows
        windows.forEach { (subWindow) in
            classs.forEach { (clas) in
                if subWindow.contentViewController?.isKind(of: clas) == true {
                    subWindow.contentViewController?.view.window?.close()
                }
            }
        }
    }
}
extension TrtcEngine:NSWindowDelegate {
    public func windowWillClose(_ notification: Notification) {
        
    }
    
    
}



